package com.tma.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    private static final String TAG = "SpinnerActivity";
    private Spinner mSpinner;
    private ArrayAdapter<CharSequence> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);

        mSpinner = (Spinner) findViewById(R.id.sp_day);

        mAdapter = ArrayAdapter.createFromResource(this, R.array.days, android.R.layout.simple_spinner_item);
        mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(mAdapter);

        // DON'T CALL setOnClickListener() on AdapterView
        // mSpinner.setOnClickListener(this);

        // setOnItemClickListener() CAN NOT BE USED WITH A SPINNER
        // mSpinner.setOnItemClickListener(this);

        mSpinner.setOnItemSelectedListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_spinner, menu);
        return true;
    }

    public void onItemSelected(AdapterView<?> adapterView, View target, int position, long id) {
        // see layout definition of android.R.layout.simple_spinner_item
        TextView tv = (TextView) target;
        Log.i(TAG, "onItemSelected: " + tv.getText().toString());
        Log.i(TAG, "onItemSelected: position = " + position + "; id = " + id);
        Toast.makeText(this, "onItemSelected: " + tv.getText().toString(), Toast.LENGTH_SHORT).show();
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
        // see layout definition of android.R.layout.simple_spinner_item
        Log.i(TAG, "onNothingSelected");
    }
}
